//
//  GRChooseGender.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/7/21.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRChooseGender.h"

@interface GRChooseGender ()

@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIView *genderView;
@property (nonatomic, strong) UIButton *maleBtn;
@property (nonatomic, strong) UIButton *femaleBtn;

@end

@implementation GRChooseGender

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self drawUI];
    }
    return self;
}

- (void)drawUI {
    _backgroundView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:_backgroundView];
    [window bringSubviewToFront:_backgroundView];

    CGFloat height  = IS_IPHONEX ? (230 + 34) : 230;
    _genderView = [[UIView alloc] initWithFrame:CGRectMake(0, KscreenHeight, KscreenWidth, height)];
    self.genderView.backgroundColor = [UIColor whiteColor];
    [self.backgroundView addSubview:self.genderView];

    UILabel *gender = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 60, 20)];
    gender.text = @"性别";
    gender.font = [UIFont systemFontOfSize:15];
    gender.textColor = HEXCOLOR(0x666666);
    [self.genderView addSubview:gender];
    
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    closeBtn.frame = CGRectMake(KscreenWidth-38, 5, 30, 30);
    [closeBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeGenderAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.genderView addSubview:closeBtn];
    
    UILabel *line_1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 39.4, KscreenWidth, 0.6)];
    line_1.backgroundColor = KGMBACKGROUND_COLOR;
    [self.genderView addSubview:line_1];
    
    UILabel *male = [[UILabel alloc] initWithFrame:CGRectMake(15, 55, 60, 20)];
    male.text = @"男";
    male.font = [UIFont systemFontOfSize:15];
    male.textColor = KGMBLACK_COLOR;
    [self.genderView addSubview:male];
    
    _maleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _maleBtn.frame = CGRectMake(KscreenWidth-38, 50, 30, 30);
    [_maleBtn setImage:[UIImage imageNamed:@"shopcar_1"] forState:UIControlStateNormal];
    [_maleBtn setImage:[UIImage imageNamed:@"shopcar_2"] forState:UIControlStateSelected];
    [_maleBtn addTarget:self action:@selector(chooseMaleBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.genderView addSubview:_maleBtn];
    
    UILabel *line_2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 89.4, KscreenWidth, 0.6)];
    line_2.backgroundColor = KGMBACKGROUND_COLOR;
    [self.genderView addSubview:line_2];

    UILabel *female = [[UILabel alloc] initWithFrame:CGRectMake(15, 105, 60, 20)];
    female.text = @"女";
    female.font = [UIFont systemFontOfSize:15];
    female.textColor = KGMBLACK_COLOR;
    [self.genderView addSubview:female];
    
    _femaleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _femaleBtn.frame = CGRectMake(KscreenWidth-38, 100, 30, 30);
    [_femaleBtn setImage:[UIImage imageNamed:@"shopcar_1"] forState:UIControlStateNormal];
    [_femaleBtn setImage:[UIImage imageNamed:@"shopcar_2"] forState:UIControlStateSelected];
    [_femaleBtn addTarget:self action:@selector(chooseFemaleBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.genderView addSubview:_femaleBtn];
    
    UILabel *line_3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 139.4, KscreenWidth, 0.6)];
    line_3.backgroundColor = KGMBACKGROUND_COLOR;
    [self.genderView addSubview:line_3];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(0, 190, KscreenWidth, 40);
    sureBtn.backgroundColor = KGMTHEM_COLOR;
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(didClickSureBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.genderView addSubview:sureBtn];
    
    self.backgroundView.hidden = YES;
}

- (void)showGender {
    WEAKSELF
    self.backgroundView.hidden = NO;
    [UIView animateWithDuration:.4 animations:^{
        weakSelf.genderView.y = KscreenHeight - weakSelf.genderView.height;
        weakSelf.backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }];
}

- (void)hideGender {
    WEAKSELF
    [UIView animateWithDuration:.4 animations:^{
        weakSelf.genderView.y = KscreenHeight;
        weakSelf.backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    } completion:^(BOOL finished) {
        weakSelf.backgroundView.hidden = YES;
    }];
}

- (void)closeGenderAction:(id)sender {
    [self hideGender];
}

- (void)chooseMaleBtn:(id)sender {
    _femaleBtn.selected = NO;
    _maleBtn.selected   = YES;
}

- (void)chooseFemaleBtn:(id)sender {
    _maleBtn.selected   = NO;
    _femaleBtn.selected = YES;
}

- (void)didClickSureBtn:(id)sender {
    if (_femaleBtn.selected || _maleBtn.selected) {
        NSString *gender = _femaleBtn.selected ? @"female" : @"male";
        if ([self.delegate respondsToSelector:@selector(didFinishChooseGender:)]) {
            [self.delegate didFinishChooseGender:gender];
            [self hideGender];
        }
    }else {
        [GRHUD showMessage:@"请选择性别"];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
